package com.cqjtu.eecs.controller;

import com.cqjtu.eecs.exception.ParamsErrorException;
import com.cqjtu.eecs.service.impl.FileServiceImpl;
import com.cqjtu.eecs.utils.Assert;
import com.cqjtu.eecs.utils.Result;
import com.cqjtu.eecs.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

/**
 * @description: 文件前端控制器
 * @author:
 * @create: 2022-03-01 14:02
 **/

@RestController
@RequestMapping("/file")
@Api(tags = "文件接口")
public class FileController {

    @Autowired
    private FileServiceImpl fileServiceImpl;

    @PostMapping("/upload/training-project")
    @ApiOperation(value = "培养方案excel上传")
    @Transactional(rollbackFor = Exception.class)
    public Result uploadTrainingProjectExcel(@RequestBody MultipartFile file){
        if (Objects.isNull(file)){
            return Result.error(ResultCode.PARAMETER_ERROR);
        }
        Boolean save = null;
        try {
            save = fileServiceImpl.uploadTrainingProject(file);
        } catch (IOException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动事务回滚
            return Result.error();
        }
        if (!save)
            return Result.error();
        return Result.success();
    }
    @PostMapping("/upload/course_outline")
    @ApiOperation("上传课程大纲 ")
    public  Result uploadCourseOutline(@RequestBody MultipartFile file){
        Assert.notNull(file, new ParamsErrorException("The file can not be null"));
        try {
            if (fileServiceImpl.uploadCourseOutline(file))
                return Result.success();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error();
    }

}
